<?php
/**
 * 医生管理
 * 州弟医院信息系统 - 州弟学安全
 */

// 包含函数文件
require_once '../includes/functions.php';

// 检查用户是否已登录
if (!isLoggedIn()) {
    $_SESSION['error_msg'] = '请先登录';
    redirect('../login.php');
}

// 检查用户是否有权限
if (!hasRole('admin')) {
    $_SESSION['error_msg'] = '您没有权限访问此页面';
    redirect('../index.php');
}

// 初始化变量
$doctors = [];
$success = '';
$error = '';
$edit_doctor = null;
$departments = [];

// 获取所有科室
$stmt = "SELECT id, name FROM departments ORDER BY name";
$dept_result = $conn->query($stmt);
if ($dept_result && $dept_result->num_rows > 0) {
    while($row = $dept_result->fetch_assoc()) {
        $departments[] = $row;
    }
}

// 处理删除医生请求
if (isset($_GET['delete']) && is_numeric($_GET['delete'])) {
    $doctor_id = (int)$_GET['delete'];
    
    // 检查医生是否存在
    $stmt = "SELECT * FROM doctors WHERE id = $doctor_id";
    $result = $conn->query($stmt);
    $doctor = $result->fetch_assoc();
    
    if (!$doctor) {
        $error = "医生不存在";
    } else {
        // 检查医生是否有关联的预约
        $stmt = "SELECT COUNT(*) as count FROM appointments WHERE doctor_id = $doctor_id";
        $result = $conn->query($stmt);
        $appointment_count = $result->fetch_assoc()['count'];
        
        if ($appointment_count > 0) {
            $error = "无法删除，该医生有 {$appointment_count} 个相关联的预约";
        } else {
            // 删除医生
            $stmt = "DELETE FROM doctors WHERE id = $doctor_id";
            $result = $conn->query($stmt);
            
            if ($result) {
                // 记录操作日志
                logAction($_SESSION['user_id'], '删除医生', "删除了医生 ID: {$doctor_id}, 姓名: {$doctor['name']}");
                $success = "医生删除成功";
            } else {
                $error = "删除失败，请稍后再试";
            }
        }
    }
}

// 处理编辑医生请求
if (isset($_GET['edit']) && is_numeric($_GET['edit'])) {
    $doctor_id = (int)$_GET['edit'];
    
    $stmt = "SELECT * FROM doctors WHERE id = $doctor_id";
    $result = $conn->query($stmt);
    
    if ($result && $result->num_rows > 0) {
        $edit_doctor = $result->fetch_assoc();
    } else {
        $error = "医生不存在";
        $edit_doctor = null;
    }
}

// 处理添加医生表单提交
if (isset($_POST['add_doctor'])) {
    $name = $conn->real_escape_string(trim($_POST['name']));
    $gender = $conn->real_escape_string($_POST['gender']);
    $department_id = (int)$_POST['department_id'];
    $title = $conn->real_escape_string(trim($_POST['title']));
    $speciality = isset($_POST['speciality']) ? $conn->real_escape_string(trim($_POST['speciality'])) : '';
    $phone = isset($_POST['phone']) ? $conn->real_escape_string(trim($_POST['phone'])) : '';
    $email = isset($_POST['email']) ? $conn->real_escape_string(trim($_POST['email'])) : '';
    $bio = isset($_POST['bio']) ? $conn->real_escape_string(trim($_POST['bio'])) : '';
    $working_days = isset($_POST['working_days']) ? $conn->real_escape_string(trim($_POST['working_days'])) : '';
    
    // 验证必填字段
    if (empty($name) || empty($department_id) || empty($title)) {
        $error = "请填写所有必填字段";
    } else {
        // 检查部门是否存在
        $stmt = "SELECT COUNT(*) as count FROM departments WHERE id = $department_id";
        $result = $conn->query($stmt);
        if ($result->fetch_assoc()['count'] == 0) {
            $error = "选择的科室不存在";
        } else {
            // 添加医生
            $stmt = "INSERT INTO doctors (name, gender, department_id, title, speciality, phone, email, bio, working_days, created_at) 
                     VALUES ('$name', '$gender', $department_id, '$title', '$speciality', '$phone', '$email', '$bio', '$working_days', NOW())";
            $result = $conn->query($stmt);
            
            if ($result) {
                $doctor_id = $conn->insert_id;
                
                // 处理医生头像上传
                if (isset($_FILES['avatar']) && $_FILES['avatar']['error'] == 0) {
                    $upload_dir = '../uploads/avatars/';
                    $file_extension = pathinfo($_FILES['avatar']['name'], PATHINFO_EXTENSION);
                    $file_name = 'doctor_' . $doctor_id . '.' . $file_extension;
                    $target_file = $upload_dir . $file_name;
                    
                    // 确保上传目录存在
                    if (!file_exists($upload_dir)) {
                        mkdir($upload_dir, 0777, true);
                    }
                    
                    // 移动上传的文件
                    if (move_uploaded_file($_FILES['avatar']['tmp_name'], $target_file)) {
                        // 更新医生的头像字段
                        $update_stmt = "UPDATE doctors SET avatar = '$file_name' WHERE id = $doctor_id";
                        $conn->query($update_stmt);
                    }
                }
                
                // 记录操作日志
                logAction($_SESSION['user_id'], '添加医生', "添加了新医生 ID: {$doctor_id}, 姓名: {$name}");
                $success = "医生添加成功";
            } else {
                $error = "添加失败，请稍后再试: " . $conn->error;
            }
        }
    }
}

// 处理编辑医生表单提交
if (isset($_POST['edit_doctor'])) {
    $doctor_id = (int)$_POST['doctor_id'];
    $name = $conn->real_escape_string(trim($_POST['name']));
    $gender = $conn->real_escape_string($_POST['gender']);
    $department_id = (int)$_POST['department_id'];
    $title = $conn->real_escape_string(trim($_POST['title']));
    $speciality = isset($_POST['speciality']) ? $conn->real_escape_string(trim($_POST['speciality'])) : '';
    $phone = isset($_POST['phone']) ? $conn->real_escape_string(trim($_POST['phone'])) : '';
    $email = isset($_POST['email']) ? $conn->real_escape_string(trim($_POST['email'])) : '';
    $bio = isset($_POST['bio']) ? $conn->real_escape_string(trim($_POST['bio'])) : '';
    $working_days = isset($_POST['working_days']) ? $conn->real_escape_string(trim($_POST['working_days'])) : '';
    
    // 验证必填字段
    if (empty($name) || empty($department_id) || empty($title)) {
        $error = "请填写所有必填字段";
    } else {
        // 检查医生是否存在
        $stmt = "SELECT * FROM doctors WHERE id = $doctor_id";
        $result = $conn->query($stmt);
        $doctor = ($result && $result->num_rows > 0) ? $result->fetch_assoc() : null;
        
        if (!$doctor) {
            $error = "医生不存在";
        } else {
            // 检查部门是否存在
            $stmt = "SELECT COUNT(*) as count FROM departments WHERE id = $department_id";
            $result = $conn->query($stmt);
            if ($result->fetch_assoc()['count'] == 0) {
                $error = "选择的科室不存在";
            } else {
                // 更新医生信息
                $stmt = "UPDATE doctors SET 
                         name = '$name', 
                         gender = '$gender', 
                         department_id = $department_id, 
                         title = '$title', 
                         speciality = '$speciality', 
                         phone = '$phone', 
                         email = '$email', 
                         bio = '$bio', 
                         working_days = '$working_days', 
                         updated_at = NOW() 
                         WHERE id = $doctor_id";
                $result = $conn->query($stmt);
                
                if ($result) {
                    // 处理医生头像上传
                    if (isset($_FILES['avatar']) && $_FILES['avatar']['error'] == 0) {
                        $upload_dir = '../uploads/avatars/';
                        $file_extension = pathinfo($_FILES['avatar']['name'], PATHINFO_EXTENSION);
                        $file_name = 'doctor_' . $doctor_id . '.' . $file_extension;
                        $target_file = $upload_dir . $file_name;
                        
                        // 确保上传目录存在
                        if (!file_exists($upload_dir)) {
                            mkdir($upload_dir, 0777, true);
                        }
                        
                        // 移动上传的文件
                        if (move_uploaded_file($_FILES['avatar']['tmp_name'], $target_file)) {
                            // 更新医生的头像字段
                            $update_stmt = "UPDATE doctors SET avatar = '$file_name' WHERE id = $doctor_id";
                            $conn->query($update_stmt);
                        }
                    }
                    
                    // 记录操作日志
                    logAction($_SESSION['user_id'], '编辑医生', "编辑了医生 ID: {$doctor_id}, 姓名: {$name}");
                    $success = "医生信息更新成功";
                    
                    // 重定向以清除表单
                    redirect('doctors.php');
                } else {
                    $error = "更新失败，请稍后再试: " . $conn->error;
                }
            }
        }
    }
}

// 获取部门ID筛选
$department_filter = isset($_GET['department']) && is_numeric($_GET['department']) ? (int)$_GET['department'] : 0;

// 构建查询条件
$where_conditions = [];
$where_clause = "";

if ($department_filter > 0) {
    $where_conditions[] = "d.department_id = $department_filter";
}

// 分页设置
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$limit = 9; // 每页显示的记录数
$offset = ($page - 1) * $limit;

// 获取医生总数
$count_sql = "SELECT COUNT(*) as total FROM doctors d";
if (!empty($where_conditions)) {
    $count_sql .= " WHERE " . implode(" AND ", $where_conditions);
}
$count_result = $conn->query($count_sql);
$total_records = $count_result->fetch_assoc()['total'];
$total_pages = ceil($total_records / $limit);

// 查询医生列表
$sql = "SELECT d.*, dp.name as department_name 
        FROM doctors d 
        LEFT JOIN departments dp ON d.department_id = dp.id";

if (!empty($where_conditions)) {
    $where_clause = " WHERE " . implode(" AND ", $where_conditions);
    $sql .= $where_clause;
}

$sql .= " ORDER BY d.name LIMIT $offset, $limit";
$result = $conn->query($sql);
$doctors = [];

if ($result && $result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $doctors[] = $row;
    }
}

// 页面标题
$page_title = '医生管理';
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $page_title; ?> - <?php echo SITE_NAME; ?></title>
    <link rel="stylesheet" href="/assets\css/bootstrap_69c9e605.min.css">
    <link rel="stylesheet" href="/assets\css/all_de197510.min.css">
    <link rel="stylesheet" href="/assets\css/style.css">
</head>
<body>
    <div class="container-fluid">
        <div class="row">
            <!-- 侧边栏 -->
            <div class="col-md-2 px-0 admin-sidebar">
                <div class="py-3 px-3 bg-dark">
                    <a href="<?php echo SITE_URL; ?>admin/" class="text-white text-decoration-none">
                        <i class="fas fa-hospital-alt mr-2"></i>管理后台
                    </a>
                </div>
                <div class="p-3">
                    <div class="text-white mb-3">
                        <i class="fas fa-user-circle mr-1"></i> <?php echo $_SESSION['username']; ?>
                    </div>
                    <ul class="nav flex-column">
                        <li class="nav-item">
                            <a class="nav-link" href="<?php echo SITE_URL; ?>admin/">
                                <i class="fas fa-tachometer-alt mr-2"></i>仪表盘
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="<?php echo SITE_URL; ?>admin/users.php">
                                <i class="fas fa-users mr-2"></i>用户管理
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="<?php echo SITE_URL; ?>admin/patients.php">
                                <i class="fas fa-user-injured mr-2"></i>患者管理
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link active" href="<?php echo SITE_URL; ?>admin/doctors.php">
                                <i class="fas fa-user-md mr-2"></i>医生管理
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="<?php echo SITE_URL; ?>admin/departments.php">
                                <i class="fas fa-hospital mr-2"></i>科室管理
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="<?php echo SITE_URL; ?>admin/appointments.php">
                                <i class="fas fa-calendar-check mr-2"></i>预约管理
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="<?php echo SITE_URL; ?>admin/medicines.php">
                                <i class="fas fa-pills mr-2"></i>药品管理
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="<?php echo SITE_URL; ?>admin/records.php">
                                <i class="fas fa-notes-medical mr-2"></i>病历管理
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="<?php echo SITE_URL; ?>admin/logs.php">
                                <i class="fas fa-history mr-2"></i>系统日志
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="<?php echo SITE_URL; ?>admin/settings.php">
                                <i class="fas fa-cog mr-2"></i>系统设置
                            </a>
                        </li>
                        <li class="nav-item mt-3">
                            <a class="nav-link" href="<?php echo SITE_URL; ?>">
                                <i class="fas fa-home mr-2"></i>返回前台
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="<?php echo SITE_URL; ?>logout.php">
                                <i class="fas fa-sign-out-alt mr-2"></i>退出登录
                            </a>
                        </li>
                    </ul>
                </div>
            </div>
            
            <!-- 主要内容区域 -->
            <div class="col-md-10 ml-sm-auto px-4 py-3">
                <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
                    <h1 class="h2"><?php echo $page_title; ?></h1>
                    <div class="btn-toolbar mb-2 mb-md-0">
                        <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#addDoctorModal">
                            <i class="fas fa-plus"></i> 添加医生
                        </button>
                    </div>
                </div>
                
                <!-- 科室筛选 -->
                <div class="mb-4">
                    <form method="get" class="form-inline">
                        <div class="form-group mr-2">
                            <label for="department" class="mr-2">按科室筛选:</label>
                            <select name="department" id="department" class="form-control" onchange="this.form.submit()">
                                <option value="0">所有科室</option>
                                <?php foreach ($departments as $department): ?>
                                    <option value="<?php echo $department['id']; ?>" <?php echo $department_filter == $department['id'] ? 'selected' : ''; ?>>
                                        <?php echo $department['name']; ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                    </form>
                </div>
                
                <?php if ($success): ?>
                    <div class="alert alert-success">
                        <?php echo $success; ?>
                    </div>
                <?php endif; ?>
                
                <?php if ($error): ?>
                    <div class="alert alert-danger">
                        <?php echo $error; ?>
                    </div>
                <?php endif; ?>
                
                <!-- 医生列表 -->
                <div class="row">
                    <?php if (empty($doctors)): ?>
                        <div class="col-12">
                            <div class="alert alert-info">暂无医生信息</div>
                        </div>
                    <?php else: ?>
                        <?php foreach ($doctors as $doctor): ?>
                            <div class="col-md-4 mb-4">
                                <div class="card h-100">
                                    <div class="card-header bg-light">
                                        <div class="d-flex justify-content-between align-items-center">
                                            <h5 class="mb-0"><?php echo $doctor['name']; ?></h5>
                                            <div>
                                                <a href="?edit=<?php echo $doctor['id']; ?>" class="btn btn-sm btn-info" title="编辑医生">
                                                    <i class="fas fa-edit"></i>
                                                </a>
                                                <a href="?delete=<?php echo $doctor['id']; ?>" class="btn btn-sm btn-danger" title="删除医生" onclick="return confirm('确定要删除此医生吗？此操作不可恢复！');">
                                                    <i class="fas fa-trash-alt"></i>
                                                </a>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="card-body">
                                        <div class="text-center mb-3">
                                            <?php if (!empty($doctor['avatar'])): ?>
                                                <img src="<?php echo SITE_URL; ?>uploads/avatars/<?php echo $doctor['avatar']; ?>" class="rounded-circle" alt="<?php echo $doctor['name']; ?>" style="width: 100px; height: 100px; object-fit: cover;">
                                            <?php else: ?>
                                                <img src="<?php echo SITE_URL; ?>assets/images/default-avatar.png" class="rounded-circle" alt="默认头像" style="width: 100px; height: 100px; object-fit: cover;">
                                            <?php endif; ?>
                                        </div>
                                        <div class="doctor-info">
                                            <p><i class="fas fa-hospital mr-2 text-muted"></i> 科室：<?php echo isset($doctor['department_name']) ? $doctor['department_name'] : '暂无'; ?></p>
                                            <p><i class="fas fa-user-md mr-2 text-muted"></i> 职称：<?php echo isset($doctor['title']) ? $doctor['title'] : '暂无'; ?></p>
                                            <p><i class="fas fa-stethoscope mr-2 text-muted"></i> 专长：<?php echo isset($doctor['speciality']) ? $doctor['speciality'] : '暂无'; ?></p>
                                            <p><i class="fas fa-phone mr-2 text-muted"></i> 电话：<?php echo isset($doctor['phone']) ? $doctor['phone'] : '暂无'; ?></p>
                                            <p><i class="fas fa-envelope mr-2 text-muted"></i> 邮箱：<?php echo isset($doctor['email']) ? $doctor['email'] : '暂无'; ?></p>
                                            <p><i class="fas fa-calendar-alt mr-2 text-muted"></i> 出诊时间：<?php echo isset($doctor['working_days']) ? $doctor['working_days'] : '暂无'; ?></p>
                                        </div>
                                    </div>
                                    <div class="card-footer bg-white">
                                        <a href="appointments.php?doctor=<?php echo $doctor['id']; ?>" class="btn btn-sm btn-outline-primary">
                                            <i class="fas fa-calendar-check mr-1"></i> 查看预约
                                        </a>
                                    </div>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </div>
                
                <!-- 分页导航 -->
                <nav aria-label="Page navigation">
                    <ul class="pagination">
                        <?php if ($page > 1): ?>
                            <li class="page-item">
                                <a class="page-link" href="doctors.php?department=<?php echo $department_filter ? '&department=' . $department_filter : ''; ?>&page=<?php echo $page - 1; ?>" aria-label="Previous">
                                    <span aria-hidden="true">&laquo;</span>
                                </a>
                            </li>
                        <?php endif; ?>
                        <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                            <li class="page-item">
                                <a class="page-link" href="doctors.php?department=<?php echo $department_filter ? '&department=' . $department_filter : ''; ?>&page=<?php echo $i; ?>">
                                    <?php echo $i; ?>
                                </a>
                            </li>
                        <?php endfor; ?>
                        <?php if ($page < $total_pages): ?>
                            <li class="page-item">
                                <a class="page-link" href="doctors.php?department=<?php echo $department_filter ? '&department=' . $department_filter : ''; ?>&page=<?php echo $page + 1; ?>" aria-label="Next">
                                    <span aria-hidden="true">&raquo;</span>
                                </a>
                            </li>
                        <?php endif; ?>
                    </ul>
                </nav>
                
                <!-- 版权信息 -->
                <footer class="mt-5 text-center text-muted">
                    <p>
                        &copy; <?php echo date('Y'); ?> <?php echo SITE_NAME; ?>. 版权所有
                        <br>
                        <small>Powered by <a href="#" class="text-muted">州弟学安全</a> | 版本 <?php echo VERSION; ?></small>
                    </p>
                </footer>
            </div>
        </div>
    </div>
    
    <!-- 添加医生模态框 -->
    <div class="modal fade" id="addDoctorModal" tabindex="-1" role="dialog" aria-labelledby="addDoctorModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-lg" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="addDoctorModalLabel">添加医生</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <form method="post" enctype="multipart/form-data">
                    <div class="modal-body">
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="name">医生姓名 <span class="text-danger">*</span></label>
                                    <input type="text" class="form-control" id="name" name="name" required>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="gender">性别</label>
                                    <select class="form-control" id="gender" name="gender">
                                        <option value="男">男</option>
                                        <option value="女">女</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="department_id">所属科室 <span class="text-danger">*</span></label>
                                    <select class="form-control" id="department_id" name="department_id" required>
                                        <option value="">选择科室</option>
                                        <?php foreach ($departments as $department): ?>
                                            <option value="<?php echo $department['id']; ?>"><?php echo $department['name']; ?></option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="title">职称 <span class="text-danger">*</span></label>
                                    <input type="text" class="form-control" id="title" name="title" placeholder="例如：主任医师" required>
                                </div>
                            </div>
                        </div>
                        
                        <div class="form-group">
                            <label for="speciality">专业特长</label>
                            <input type="text" class="form-control" id="speciality" name="speciality" placeholder="例如：心血管疾病诊断与治疗">
                        </div>
                        
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="phone">联系电话</label>
                                    <input type="text" class="form-control" id="phone" name="phone">
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="email">电子邮箱</label>
                                    <input type="email" class="form-control" id="email" name="email">
                                </div>
                            </div>
                        </div>
                        
                        <div class="form-group">
                            <label for="bio">个人简介</label>
                            <textarea class="form-control" id="bio" name="bio" rows="3"></textarea>
                        </div>
                        
                        <div class="form-group">
                            <label for="working_days">出诊时间</label>
                            <input type="text" class="form-control" id="working_days" name="working_days" placeholder="例如：周一、周三全天">
                        </div>
                        
                        <div class="form-group">
                            <label for="avatar">医生头像</label>
                            <input type="file" class="form-control-file" id="avatar" name="avatar" accept="image/*">
                            <small class="form-text text-muted">建议上传正方形图片，大小不超过2MB</small>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                        <button type="submit" name="add_doctor" class="btn btn-primary">添加</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    
    <!-- 编辑医生模态框 -->
    <?php if ($edit_doctor): ?>
    <div class="modal fade" id="editDoctorModal" tabindex="-1" role="dialog" aria-labelledby="editDoctorModalLabel" aria-hidden="true" data-show="true">
        <div class="modal-dialog modal-lg" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="editDoctorModalLabel">编辑医生</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <form method="post" enctype="multipart/form-data">
                    <div class="modal-body">
                        <input type="hidden" name="doctor_id" value="<?php echo $edit_doctor['id']; ?>">
                        
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="edit_name">医生姓名 <span class="text-danger">*</span></label>
                                    <input type="text" class="form-control" id="edit_name" name="name" value="<?php echo $edit_doctor['name']; ?>" required>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="edit_gender">性别</label>
                                    <select class="form-control" id="edit_gender" name="gender">
                                        <option value="男" <?php echo $edit_doctor['gender'] == '男' ? 'selected' : ''; ?>>男</option>
                                        <option value="女" <?php echo $edit_doctor['gender'] == '女' ? 'selected' : ''; ?>>女</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="edit_department_id">所属科室 <span class="text-danger">*</span></label>
                                    <select class="form-control" id="edit_department_id" name="department_id" required>
                                        <option value="">选择科室</option>
                                        <?php foreach ($departments as $department): ?>
                                            <option value="<?php echo $department['id']; ?>" <?php echo $edit_doctor['department_id'] == $department['id'] ? 'selected' : ''; ?>>
                                                <?php echo $department['name']; ?>
                                            </option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="edit_title">职称 <span class="text-danger">*</span></label>
                                    <input type="text" class="form-control" id="edit_title" name="title" value="<?php echo $edit_doctor['title']; ?>" required>
                                </div>
                            </div>
                        </div>
                        
                        <div class="form-group">
                            <label for="edit_speciality">专业特长</label>
                            <input type="text" class="form-control" id="edit_speciality" name="speciality" value="<?php echo isset($edit_doctor['speciality']) ? $edit_doctor['speciality'] : ''; ?>">
                        </div>
                        
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="edit_phone">联系电话</label>
                                    <input type="text" class="form-control" id="edit_phone" name="phone" value="<?php echo isset($edit_doctor['phone']) ? $edit_doctor['phone'] : ''; ?>">
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label for="edit_email">电子邮箱</label>
                                    <input type="email" class="form-control" id="edit_email" name="email" value="<?php echo isset($edit_doctor['email']) ? $edit_doctor['email'] : ''; ?>">
                                </div>
                            </div>
                        </div>
                        
                        <div class="form-group">
                            <label for="edit_bio">个人简介</label>
                            <textarea class="form-control" id="edit_bio" name="bio" rows="3"><?php echo isset($edit_doctor['bio']) ? $edit_doctor['bio'] : ''; ?></textarea>
                        </div>
                        
                        <div class="form-group">
                            <label for="edit_working_days">出诊时间</label>
                            <input type="text" class="form-control" id="edit_working_days" name="working_days" value="<?php echo isset($edit_doctor['working_days']) ? $edit_doctor['working_days'] : ''; ?>">
                        </div>
                        
                        <div class="form-group">
                            <label for="edit_avatar">医生头像</label>
                            <?php if (!empty($edit_doctor['avatar'])): ?>
                                <div class="mb-2">
                                    <img src="<?php echo SITE_URL; ?>uploads/avatars/<?php echo $edit_doctor['avatar']; ?>" alt="当前头像" class="img-thumbnail" style="max-width: 100px;">
                                </div>
                            <?php endif; ?>
                            <input type="file" class="form-control-file" id="edit_avatar" name="avatar" accept="image/*">
                            <small class="form-text text-muted">上传新头像将替换当前头像。留空则保持不变。</small>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                        <button type="submit" name="edit_doctor" class="btn btn-primary">保存</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <script>
        $(document).ready(function() {
            $('#editDoctorModal').modal('show');
        });
    </script>
    <?php endif; ?>

    <script src="/assets\js/jquery-3_7b53a2dc.5.1.min.js"></script></script></script></script>
    <script src="/assets\js/popper_9c21e708.min.js"></script></script></script></script>
    <script src="/assets\js/bootstrap_a618e9ff.min.js"></script></script></script></script>
    <script src="/assets\js/main.js"></script></script>
</body>
</html> 